﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class UserGroupController : Base
    {
        private readonly AdminUserGroupService adminUserGroupService;
        private readonly LogService _logService;
        public UserGroupController(AdminUserGroupService adminUserGroup, LogService logService)
        {
            adminUserGroupService = adminUserGroup;
            _logService = logService;
        }

        public async Task<IActionResult> IndexAsync(int json = 0)
        {
            if (json == 1)
            {
                var list = await adminUserGroupService.GetListAsync();
                return Json(new { code = 0, data = list });
            }
            return View();
        }

        public async Task<IActionResult> AddOrEditAsync(int? id, int edit = 0)
        {
            if (edit == 1)
            {
                var data = await adminUserGroupService.GetAsync(id);
                EditAdminUserGroup viewAdminUserGroup = new()
                {
                    Id = data.Id,
                    Groupname = data.Groupname,
                    Systemrole = data.Systemrole,
                    Channlrole = data.Channlrole
                };
                return Json(new { status = 1, data = viewAdminUserGroup });
            }
            ViewBag.id = id;
            return View();
        }

        [HttpPost, ActionName("CreatePost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> CreatePostAsync(EditAdminUserGroup adminUserGroup)
        {

            if (string.IsNullOrEmpty(adminUserGroup.Groupname))
                return Json(new { status = 0, msg = "用户组名不能为空！" });

            if (await adminUserGroupService.GetByGroupNameAsync(adminUserGroup.Groupname) == false)
                return Json(new { status = 0, msg = "该用户组名已存在，请重新输入！" });

            AdminUserGroup admin = new()
            {
                Groupname = adminUserGroup.Groupname,
                Systemrole = JsonSerializer.Serialize(adminUserGroup.Systemrole),
                Channlrole = JsonSerializer.Serialize(adminUserGroup.Channlrole),
            };
            await adminUserGroupService.InsertAsync(admin);
            await _logService.InsertLogSqlAsync(await UserInfoAsync(), "新增用户组");
            return Json(new { status = 1, msg = "添加成功！" });

        }


        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost, ActionName("EditPost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> EditPost(EditAdminUserGroup adminUserGroup, int id)
        {
            if (ModelState.IsValid)
            {
                var get = await adminUserGroupService.GetAsync(id);
                if (get == null)
                    return Json(new { status = 0, msg = "请勿非法提交！" });

                if (string.IsNullOrEmpty(adminUserGroup.Groupname))
                    return Json(new { status = 0, msg = "用户组名不能为空！" });

                if (adminUserGroup.Groupname != get.Groupname)
                    if (await adminUserGroupService.GetByGroupNameAsync(adminUserGroup.Groupname) == false)
                        return Json(new { status = 0, msg = "该用户组名已存在，请重新输入！" });

                //string[] systemrole = adminUserGroup.Systemrole;
                //string[] syst = new string[10];

                //for (int i =0; i<= systemrole.Length; i++)
                //{
                //    syst[i] = systemrole[i];
                //}

                get.Groupname = adminUserGroup.Groupname;
                get.Systemrole = adminUserGroup.Systemrole;
                get.Channlrole = adminUserGroup.Channlrole;

                await adminUserGroupService.UpdateAsync(get, id);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "修改用户组 ID：" + id);
                return Json(new { status = 1, msg = "修改成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            if (await adminUserGroupService.DeleteByPriKeyAsync(id))
            {
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "删除用户组 ID：" + id);
                return Json(new { status = 1, msg = "删除成功！" });
            }
            else
                return Json(new { status = 0, msg = "删除失败！可能该角色下分配有用户" });
        }

    }
}
